/*
    Copyright (C) 2015 Apple Inc. All Rights Reserved.
    See LICENSE.txt for this sample’s licensing information
    
    Abstract:
    A table view controller that displays filtered strings (used by other view controllers for simple displaying and filtering of data).
*/

import UIKit

class SearchControllerBaseViewController: UITableViewController, UISearchBarDelegate{
    // MARK: Types
    
    struct TableViewConstants {
        static let tableViewCellIdentifier = "searchResultsCell"
    }
    
      var searchControllerbase: UISearchController!
      var allcourse:AllCourseViewController! = nil
    
    
   // var searchResultsController: SearchResultsViewController!
    
    // MARK: Properties
   // let allResults1 = ["test", "to", "the"]
    
   var searchhistory:NSMutableArray = []
    
    
    let allResults = ["Here's", "to", "the", "crazy", "ones."]

    lazy var visibleResults: [String] = self.allResults

    /// A `nil` / empty filter string means show all results. Otherwise, show only results containing the filter.
    var filterString: String? = nil {
        didSet {
            if filterString == nil || filterString!.isEmpty {
                visibleResults = allResults
            }
            else {
                // Filter the results using a predicate based on the filter string.
                let filterPredicate = NSPredicate(format: "self contains[c] %@", argumentArray: [filterString!])

                visibleResults = allResults.filter { filterPredicate.evaluateWithObject($0) }
            }

            tableView.reloadData()
        }
        
        
    }
    
    // MARK: UITableViewDataSource
    
    override func viewDidLoad() {
       // visibleResults =
        
        var userDefault = NSUserDefaults.standardUserDefaults()
        var searchArray:NSArray = []
        if(userDefault.objectForKey("search history") != nil)
        {
          searchArray = userDefault.objectForKey("search history") as! NSArray
        }
        
        searchhistory = searchArray.mutableCopy() as! NSMutableArray
        
       //  if(searchhistory )
      //  {
      //      searchhistory.addObject("test")
      //  }
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        //return visibleResults.count
        return searchhistory.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCellWithIdentifier(TableViewConstants.tableViewCellIdentifier, forIndexPath: indexPath) as! UITableViewCell
    }
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        //cell.textLabel!.text = visibleResults[indexPath.row]
        cell.textLabel!.text = searchhistory.objectAtIndex(indexPath.row) as? String
        
       // [indexPath.row]
    }
   
   override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
       // super.viewDidLoad()
        
        // Create the search results view controller and use it for the UISearchController.
        
      //  tableView.deselectRowAtIndexPath(indexPath, animated: true)
        
    
   

    }

}
